package zencstudio.sgrealtor.utils;

import java.util.ArrayList;
import java.util.List;

import zencstudio.sgrealtor.view.phone.ContactActivity;
import zencstudio.sgrealtor.view.phone.FeedBackActivity;
import zencstudio.sgrealtor.view.phone.ListCourseActivity;
import zencstudio.sgrealtor.view.phone.LoanCalculator;
import zencstudio.sgrealtor.view.phone.MainActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import zencstudio.sgrealtor.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class WFSlidingMenu extends SlidingMenu{

	private ListView lvMenu;
	SlidingMenuAdapter adapter;
	public WFSlidingMenu(Activity activity, int slideStyle) {
		super(activity, slideStyle);
	}
	public WFSlidingMenu(Context context) {
		super(context, null);
	}
	public WFSlidingMenu(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
	}
	public WFSlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context,attrs,defStyle);
	}
	public void setControls(){
		lvMenu = (ListView) findViewById(R.id.lvMenu);
		List<String> menuItems = new ArrayList<String>();
		menuItems.add(getContext().getString(R.string.home));
		menuItems.add(getContext().getString(R.string.cource));
		//menuItems.add(getContext().getString(R.string.joinus));
		menuItems.add(getContext().getString(R.string.feedback));
		menuItems.add(getContext().getString(R.string.loan_calculator));
		menuItems.add(getContext().getString(R.string.contactus));
		adapter = new SlidingMenuAdapter(getContext(), menuItems);
		lvMenu.setAdapter(adapter);
		lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String selectedItem=adapter.arrs.get(arg2).toString();
				if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.home))){
					Intent intent = new Intent(getContext(),MainActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getContext().startActivity(intent);
				}
				else if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.cource))){
					Intent intent = new Intent(getContext(),ListCourseActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getContext().startActivity(intent);
				}
				else if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.joinus))){
					
				}
				else if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.contactus))){
					Intent intent = new Intent(getContext(),ContactActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getContext().startActivity(intent);
				}
				else if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.feedback))){
					Intent intent = new Intent(getContext(),FeedBackActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getContext().startActivity(intent);
				}
				else if(selectedItem.equalsIgnoreCase(getContext().getString(R.string.loan_calculator))){
					Intent intent = new Intent(getContext(),LoanCalculator.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getContext().startActivity(intent);
				}
			}
		});
	}
	
	class SlidingMenuAdapter extends BaseAdapter{
		private List<String>arrs;
		private Context context;
		public SlidingMenuAdapter(Context c,List<String>strs) {
			this.arrs = strs;
			this.context = c;
		}
		@Override
		public int getCount() {
			return arrs.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arrs.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			LayoutInflater inflater = 
		              (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if(convertView == null){
				convertView = inflater.inflate(R.layout.slidemenu_item, arg2,false);
			}
			TextView txt = (TextView) convertView.findViewById(R.id.txtName);
			txt.setText(getItem(arg0).toString());			
			return convertView;
		}
		
	}
}
